import logging
import time

import allure
from selenium.common import TimeoutException
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from utils.step_logger import step_logger


class Keywords:


    def __init__(self,driver):
        self.driver=driver

    #定位元素
    def find(self,step):
        wait=WebDriverWait(self.driver,10)
        locator=(step["by"],step["value"])
        locator2= (step["by"], step["value"])

        try:
            if step["index"] is None:
                return wait.until(EC.presence_of_element_located(locator))
            else:
                element=wait.until(EC.presence_of_all_elements_located(locator))
                return element[step['index']]
        except TimeoutException:
            logging.info(f"❌❌❌❌❌元素定位失败,元素定位信息为{locator}")

    #打开
    @step_logger
    def open(self,step):
        # with allure.step(f'第{step["step_num"]}步：{step["step_name"]}'):
        #     logging.info(f'第{step["step_num"]}步：{step["step_name"]}')
            self.driver.get(step["data"])

    #输入
    @step_logger
    def input(self,step):
        # with allure.step(f'第{step["step_num"]}步：{step["step_name"]}'):
        #     logging.info(f'第{step["step_num"]}步：{step["step_name"]}')
            self.find(step).send_keys(step["data"])
            # self.driver.find_element(step["by"],step["value"]).send_keys(step["data"])

    #等待
    @step_logger
    def wait(self,step):
        # with allure.step(f'第{step["step_num"]}步：{step["step_name"]}'):
        #     logging.info(f'第{step["step_num"]}步：{step["step_name"]}')
            time.sleep(step["data"])

    #点击
    @step_logger
    def click(self,step):
        # with allure.step(f'第{step["step_num"]}步：{step["step_name"]}'):
        #     logging.info(f'第{step["step_num"]}步：{step["step_name"]}')
            self.find(step).click()

    #清空
    @step_logger
    def clear(self,step):
        # with allure.step(f'第{step["step_num"]}步：{step["step_name"]}'):
        #     logging.info(f'第{step["step_num"]}步：{step["step_name"]}')
            self.find(step).clear()

    #截图
    @step_logger
    def shot(self,step):
        now_time=time.strftime("%Y-%m-%d %H_%M_%S")
        png=self.driver.get_screenshot_as_png()
        allure.attach(
            png,#附件内容
            f'第{step["step_num"]}步：{step["step_name"]}',#附件的名字
            attachment_type=allure.attachment_type.PNG #附件的格式
        )

    #刷新
    @step_logger
    def refresh(self,step):
        self.driver.refresh()

    #前进
    @step_logger
    def forward(self,step):
        self.driver.forward()

    #返回
    @step_logger
    def back(self,step):
        self.driver.back()

    #句柄跳转
    @step_logger
    def switch_to_window(self,step):
        handels=self.driver.window_handles
        self.driver.switch_to_window(handels[step["data"]])

    #下拉框
    @step_logger
    def select_by_value(self,step):
        select=Select(self.find(step))
        select.select_by_value(step["value"])


    #弹窗接受
    @step_logger
    def accept_alert(self,step):
        self.driver.switch_to.alert.accept()

    # 弹窗拒绝
    @step_logger
    def dismiss_alert(self,step):
        self.driver.switch_to.alert.dismiss()

    #滚动
    @step_logger
    def scroll_to(self,step):
        position_dict=eval(step["data"])
        js=f"window.scrollTo({position_dict["x"]},{position_dict["y"]});"
        self.driver.execute_script(js)

    #执行js代码
    @step_logger
    def scroll(self,step):
        self.driver.execute_script(step["data"])

    #双击
    @step_logger
    def double_click(self,step):
        element=self.find(step)
        ActionChains(self.driver).double_click(element).perform()

    #右击
    @step_logger
    def double_click(self,step):
        element=self.find(step)
        ActionChains(self.driver).context_click(element).perform()

    #悬停
    @step_logger
    def hover(self,step):
        hover_element=self.find(step)
        ActionChains(self.driver).move_to_element(hover_element).perform()

    #拖拽
    @step_logger
    def drag_and_drop(self,step):
        #{by.xpath,//input}
        source_element=self.find(step)

        target=eval(step["data"])

        target_element=self.driver.find_element(target["by"],target["value"])

        ActionChains(self.driver).drag_and_drop(source_element,target_element).perform()

    #回车
    @step_logger
    def enter(self,step):
        enter_element=self.find(step)
        enter_element.send_keys(Keys.ENTER)

    #切换frame
    @step_logger
    def switch_to_frame(self,step):
        element=self.find(step)
        self.driver.switch_to.frame(element)

    #切换frame
    @step_logger
    def switch_to_default_frame(self,step):
        self.driver.switch_to.default_content()














